package com.achen.jobs;

import com.achen.constant.RedisConstant;
import com.achen.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * 自定义job，实现定时处理垃圾图片
 * @author achen
 * @create 2020-04-12 0:43
 */
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){
        System.out.println("清理垃圾图片");
        //根据redis中保存的两个set集合的差值计算，获得垃圾图片的名称及和
        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if(set != null && set.size() > 0){
            for(String fileName : set){
                //删除七牛云服务器上的垃圾图片
                QiniuUtils.deleteFileFromQiniu(fileName);
                //删除redis集合中图片名称
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,fileName);

                System.out.println("自定义任务执行，定理垃圾图片"+fileName);
            }
        }
    }
}
